<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>客户管理</el-breadcrumb-item>
      <el-breadcrumb-item>长者标签</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="20">
      <el-col :span="4">
        <el-button type="primary" @click="openAddLabelDialog">添加标签</el-button>
      </el-col>
      <el-col :span="4" :offset="16">
        <el-input placeholder="请输入标签名" clearable v-model="labeltype" @clear="search">
          <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-table
        :data="LabelList"
        style="width: 100%;text-align: center">
      <el-table-column label="编号" type="index" align="center"></el-table-column>
      <el-table-column
          prop="labeltype"
          label="标签"
          align="center">
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="openEdit(scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6 , 12, 24]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    ></el-pagination>

    <!--    添加标签-->
    <el-dialog title="添加标签" :visible.sync="addLabelDialogVisible"
               width="50%" @close="addLabelFormClose" :close-on-click-modal="false">
      <el-form
          :model="addLabelForm"
          :rules="addLabelFormRules"
          ref="addLabelFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="标签" prop="labeltype">
          <el-input v-model="addLabelForm.labeltype"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addLabel">确 定</el-button>
        <el-button @click="addLabelFormClose">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 修改标签   -->
    <el-dialog title="修改标签" :visible.sync="updateLabelDialogVisible"
               width="50%" @close="updateLabelFormClose" :close-on-click-modal="false">
      <el-form
          :model="updateLabelForm"
          :rules="updateLabelFormRules"
          ref="updateLabelFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="标签" prop="labeltype">
          <el-input v-model="updateLabelForm.labeltype"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateLabel">确 定</el-button>
        <el-button @click="updateLabelFormClose">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      LabelList:[],
      pageNum: 1,
      pageSize: 3,
      total:9,
      labeltype:'',
      label:{},
      addLabelDialogVisible:false,
      addLabelForm:{
        labeltype:'',
      },
      addLabelFormRules:{
        labeltype: { required: true, message: "请输入标签名", trigger: "blur" },

      },
      updateLabelDialogVisible:false,
      updateLabelForm:{
        labeltype:'',
      },
      updateLabelFormRules:{
        labeltype: { required: true, message: "请输入标签名", trigger: "blur" },

      },
    }
  },
  methods:{
    //增加
    openAddLabelDialog(){
      this.addLabelForm = {},
          this.addLabelDialogVisible = true;
    },
    addLabelFormClose(){
      this.addLabelForm = {};
      this.addLabelDialogVisible = false;
    },
    addLabel(){
      let url = "/label/insertLabel";
      this.$refs.addLabelFormRef.validate((val)=>{
        if(val){
          this.$axios.post(url,this.addLabelForm).then((res)=>{
            if(res.data.code == 200) {
              this.$message({
                message: res.data.msg,
                type: 'success',
                duration: 1000
              });
              this.addLabelDialogVisible = false;
              this.initLabelData();
            }else {
              this.$message({
                message: res.data.msg,
                type:'error',
                duration:1000
              })
            }
          })
        }else {
          alert("请完善信息")
        }
      })
    },
    //修改
    openEdit(Label){
      this.updateLabelForm.id = Label.id;
      this.updateLabelForm.labeltype = Label.labeltype;
      this.updateLabelDialogVisible = true;
    },
    updateLabelFormClose(){
      this.updateLabelDialogVisible = false;
    },
    updateLabel(){
      let url = "/label/updateLabel";
      this.$refs.updateLabelFormRef.validate((val)=>{
        if(val){
          this.$axios.post(url,this.updateLabelForm).then((res)=>{
            if(res.data.code == 200) {
              this.$message({
                message: res.data.msg,
                type: 'success',
                duration: 1000
              });
              this.updateLabelDialogVisible = false;
              this.initLabelData();
            }else {
              this.$message({
                message: res.data.msg,
                type:'error',
                duration:1000
              })
            }
          })
        }else {
          alert("请完善信息")
        }
      })
    },
    handleDelete(Label){
      let url = "/label/deleteLabel";
      this.$axios.post(url,Label).then((res)=>{
        if(res.data.code == 200){
          this.$message({
            message: res.data.msg,
            type:'success',
            duration:1000
          });
          this.initLabelData();
        }else {
          this.$message({
            message: res.data.msg,
            type:'error',
            duration:1000
          })
        }
      })
    },
    search() {
      this.pageNum = 1;
      this.initLabelData();
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.pageNum = 1;
      this.initLabelData();
    },
    handleCurrentChange(newPage) {
      //alert(newPage);
      this.pageNum = newPage;
      this.initLabelData();
    },
    initLabelData(){
      let url = "/label/selectLabelByCons?now="+this.pageNum+"&size="+this.pageSize+"&labeltype="+this.labeltype;
      this.$axios.get(url).then((res)=>{
        if(res.data.code == 200){
          this.LabelList = res.data.data.list;
          this.total = res.data.data.total;
        }else {
          this.$message({
            message: res.data.msg,
            type:'error',
            duration:1000
          })
        }
      })
    }
  },
  created() {
    this.initLabelData();
  }
}
</script>

<style scoped>

</style>